﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for LienKetWebDAO
/// </summary>
public class LienKetWebDAO
{
    DataProvider provider;
	public LienKetWebDAO()
	{
        provider = new DataProvider();
	}

    public List<LienKetWeb> HienThiLienKetWeb()
    {
        List<LienKetWeb> list = new List<LienKetWeb>();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_HienThiLienKetWeb", provider.Conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            cmd.Connection.Close();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                LienKetWeb dto = new LienKetWeb();
                dto.IDLienKet = Convert.ToInt32(ds.Tables[0].Rows[i]["IDLienKet"].ToString());
                dto.HinhAnh = ds.Tables[0].Rows[i]["HinhAnh"].ToString();
                dto.UrlLienKet = ds.Tables[0].Rows[i]["UrlLienKet"].ToString();
                dto.ChuThich = ds.Tables[0].Rows[i]["ChuThich"].ToString();
                dto.HienThi = Convert.ToInt32(ds.Tables[0].Rows[i]["HienThi"].ToString());
                list.Add(dto);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return list;
    }
    //ADD Hieu-HV 30/10/2011 START
    public LienKetWeb HienThiChiTietLienKetWeb(int idLienKetWeb)
    {
        LienKetWeb dto = new LienKetWeb();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_HienThiChiTietLienKetWeb", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = idLienKetWeb;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            adapter.SelectCommand = cmd;
            adapter.Fill(ds);
            provider.Conn.Close();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dto.IDLienKet = Convert.ToInt32(ds.Tables[0].Rows[i]["IDLienKet"].ToString());
                dto.HinhAnh = ds.Tables[0].Rows[i]["HinhAnh"].ToString();
                dto.UrlLienKet = ds.Tables[0].Rows[i]["UrlLienKet"].ToString();
                dto.ChuThich = ds.Tables[0].Rows[i]["ChuThich"].ToString();
                dto.HienThi = Convert.ToInt32(ds.Tables[0].Rows[i]["HienThi"].ToString());
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return dto;
    }
    public int LayMaxIDLienKetWeb()
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_MaxIDLienKetWeb", provider.Conn);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 6));
            cmd.Parameters["@ID"].Direction = ParameterDirection.Output;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();

            kq = int.Parse(cmd.Parameters["@ID"].Value.ToString());

        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }
    public int ThemLienKetWeb(LienKetWeb linkWeb)
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_ThemLienKetWeb", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = linkWeb.IDLienKet;
            cmd.Parameters.Add("@HinhAnh", SqlDbType.VarChar).Value = linkWeb.HinhAnh;
            cmd.Parameters.Add("@UrlLienKet", SqlDbType.VarChar).Value = linkWeb.UrlLienKet;
            cmd.Parameters.Add("@ChuThich", SqlDbType.NText).Value = linkWeb.ChuThich;
            cmd.Parameters.Add("@HienThi", SqlDbType.Int).Value = linkWeb.HienThi;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            kq = 1;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }
    public int CapNhatLienKetWeb(LienKetWeb linkWeb)
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_ThemLienKetWeb", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = linkWeb.IDLienKet;
            cmd.Parameters.Add("@HinhAnh", SqlDbType.VarChar).Value = linkWeb.HinhAnh;
            cmd.Parameters.Add("@UrlLienKet", SqlDbType.VarChar).Value = linkWeb.UrlLienKet;
            cmd.Parameters.Add("@ChuThich", SqlDbType.NText).Value = linkWeb.ChuThich;
            cmd.Parameters.Add("@HienThi", SqlDbType.Int).Value = linkWeb.HienThi; 
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            kq = 1;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }
    public int XoaLienKetWeb(int id)
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_XoaLienKetWeb", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            kq = 1;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }
    //ADD Hieu-HV 30/10/2011 END
}